package com.mall.cart.web;

import com.mall.cart.dto.UserInfoDto;
import com.mall.cart.interceptor.CartIndexInterceptor;
import com.mall.cart.service.CartService;
import com.mall.cart.vo.CartItemVo;
import com.mall.cart.vo.CartVo;
import com.mall.common.constant.AuthorConstant;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author: 谢君臣
 * @Date: 2022/8/8-14:15
 * @version: 1.0
 * @function:
 */
@Controller
public class CartIndexController {

    @Resource
    private CartService cartService;

    @GetMapping({"/","/cartList","/cartList.html"})
    public String cartPage(HttpServletRequest request,HttpServletResponse response, HttpSession session, Model model){
        UserInfoDto userInfoDto = CartIndexInterceptor.threadLocal.get();
        // 获取购物车
        CartVo cartVo = cartService.getCart();
        if (userInfoDto.isTemp()){
            Cookie cookie = new Cookie(AuthorConstant.USER_KEY, userInfoDto.getUserKey());
            cookie.setMaxAge(AuthorConstant.USER_KEY_TIME_OUT);
            response.addCookie(cookie);
        }
        model.addAttribute("cartList",cartVo);
        return "cartList";
    }

    @PostMapping("/cart/addToCart")
    public String addToCart(@RequestParam(value = "skuId",required = true) Long skuId,
                            @RequestParam(value = "num",required = true)int num,
                            RedirectAttributes model){
        cartService.addToCart(skuId,num);
        model.addAttribute("skuId",skuId);
        return "redirect:http://cart.mall.com/success";
    }


    @GetMapping("/cart/changeItemCount")
    public String changeItemCount(@RequestParam(value = "skuId",required = true) Long skuId,
                            @RequestParam(value = "num",required = true)Integer num,
                            RedirectAttributes model){
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.mall.com/cartList";
//        CartVo cartVo = cartService.getCart();
//        model.addAttribute("cartList",cartVo);
//        return "cartList";
    }


    @GetMapping("/cart/delete")
    public String delete(@RequestParam(value = "skuId",required = true) Long skuId,
                                  RedirectAttributes model){
        cartService.deleteBySkuId(skuId);
//        CartVo cartVo = cartService.getCart();
//        model.addAttribute("cartList",cartVo);
//        return "cartList";
        return "redirect:http://cart.mall.com/cartList";
    }

    @GetMapping("/success")
    public String toSuccess(@RequestParam(value = "skuId",required = true) Long skuId,
                            Model model){
        CartItemVo cartItemVo = cartService.getBySkuId(skuId);
        model.addAttribute("skuInfo",cartItemVo);
        return "success";
    }

    @PostMapping("/cart/checkCart")
    public String checkCart(@RequestParam("isChecked")Integer isChecked,
                            @RequestParam("skuId")Long skuId,
                            Model model){
        cartService.checkCart(isChecked,skuId);
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cartList",cartVo);
        return "cartList";
    }

}
